package com.iflytek.exam.service.impl;


import com.iflytek.common.utils.SecurityUtils;
import com.iflytek.exam.domain.*;
import com.iflytek.exam.domain.Choice;
import com.iflytek.exam.domain.Judge;
import com.iflytek.exam.domain.QuestionExam;
import com.iflytek.exam.domain.ShortAnswer;
import com.iflytek.exam.dto.ChoiceDTO;
import com.iflytek.exam.dto.JudgeDTO;
import com.iflytek.exam.dto.ShortAnswerDTO;
import com.iflytek.exam.mapper.ChoiceMapper;
import com.iflytek.exam.mapper.JudgeMapper;
import com.iflytek.exam.mapper.QuestionExamMapper;
import com.iflytek.exam.mapper.ShortAnswerMapper;
import com.iflytek.exam.service.ICreateExamService;
import com.iflytek.exam.service.QuestionExamService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class QuestionExamServiceServiceImpl implements QuestionExamService {

    @Autowired
    QuestionExamMapper questionExamMapper;

    @Autowired
    private ChoiceMapper choiceMapper;

    @Autowired
    JudgeMapper judgeMapper;

    @Autowired
    ShortAnswerMapper shortAnswerMapper;

    @Autowired
    ICreateExamService examService;

    /**
     * 查询试题和试卷中间
     *
     * @param examId 试题和试卷中间主键
     * @return 试题和试卷中间
     */
    @Override
    public QuestionExam selectQuestionExamByExamId(Long examId) {
        return questionExamMapper.selectQuestionExamById(examId);
    }


    @Override
    public Map<String, Object> selectPaperQuestionById(Long examId, Long userId) {
        List<QuestionExam> questionExamList = questionExamMapper.selectQuestionExamListByExamIdAndUserId(examId, userId);
        List<ChoiceDTO> singleChoiceList = new ArrayList<>();
        List<ChoiceDTO> multipleChoiceList = new ArrayList<>();
        List<JudgeDTO> judgeList = new ArrayList<>();
        List<ShortAnswerDTO> shortAnswerList = new ArrayList<>();
        if (questionExamList != null) {
            questionExamList.forEach(questionExam -> {
                System.out.println(questionExam);
                if (questionExam.getType() == 1) {
                    Choice singleChoice = choiceMapper.selectChoiceById(questionExam.getQuestionId());
                    ChoiceDTO choiceDTO = new ChoiceDTO();
                    BeanUtils.copyProperties(singleChoice, choiceDTO);
                    singleChoiceList.add(choiceDTO);
                } else if (questionExam.getType() == 2) {
                    Choice multipleChoice = choiceMapper.selectChoiceById(questionExam.getQuestionId());
                    ChoiceDTO choiceDTO = new ChoiceDTO();
                    BeanUtils.copyProperties(multipleChoice, choiceDTO);
                    multipleChoiceList.add(choiceDTO);
                } else if (questionExam.getType() == 3) {
                    Judge judge = judgeMapper.selectJudgeById(questionExam.getQuestionId());
                    JudgeDTO judgeDTO = new JudgeDTO();
                    BeanUtils.copyProperties(judge, judgeDTO);
                    judgeList.add(judgeDTO);
                } else if (questionExam.getType() == 4) {
                    ShortAnswer shortAnswer = shortAnswerMapper.selectShortAnswerById(questionExam.getQuestionId());
                    ShortAnswerDTO shortAnswerDTO = new ShortAnswerDTO();
                    BeanUtils.copyProperties(shortAnswer, shortAnswerDTO);
                    shortAnswerList.add(shortAnswerDTO);
                }
            });

        }

        CreateExam exam = examService.selectcreateExamById(examId);
        Map<String, Object> map = new HashMap<>();
        map.put("examId", examId);
        map.put("time", exam.getTime());
        map.put("single", singleChoiceList);
        map.put("multiple", multipleChoiceList);
        map.put("judge", judgeList);
        map.put("shortAnswer", shortAnswerList);
        System.out.println(map);
        return map;
    }

    @Override
    public QuestionExam selectPaperQuestionByIdAndType(QuestionExam questionExam) {
        return questionExamMapper.selectPaperQuestionByIdAndType(questionExam);
    }

    @Override
    public int insertQuestionExam(QuestionExam questionExam) {
        return questionExamMapper.insertQuestionExam(questionExam);
    }

    @Override
    public Boolean deleteQuestionExamByExamId(Long examId) {
        return questionExamMapper.deleteQuestionExamByExamId(examId);
    }

    @Override
    public List<QuestionExam> selectPaperQuestionByUserId(Long userId) {
        return questionExamMapper.selectPaperQuestionByUserId(userId);
    }

    @Override
    public List<QuestionExam> selectPaperQuestionForAll() {
        return questionExamMapper.selectPaperQuestionForAll();
    }


}
